package cn.innosmart.aq.customize.datetimepicker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import cn.innosmart.aq.R;
import cn.innosmart.aq.customize.addview.AddProperty.Property;

/**
 * Created by David on 2015/12/3.
 */
public class CustomPickerDialog implements DatePicker.OnDateChangedListener, TimePicker.OnTimeChangedListener, NumberPicker.Formatter, NumberPicker.OnScrollListener, NumberPicker.OnValueChangeListener {
    private AlertDialog ad;
    private String dateTime;
    private String initDateTime;
    private Activity activity;
    private NumberPicker yearPicker;
    private NumberPicker monthPicker;
    private NumberPicker dayPicker;
    private NumberPicker hourPicker;
    private NumberPicker minutePicker;

    /**
     * 日期时间弹出选择框构
     *
     * @param activity：调用的父activity
     */
    public CustomPickerDialog(Activity activity) {
        this.activity = activity;
    }


    /**
     * 弹出日期时间选择框
     *
     * @param dateTimeTextEdite 需要设置的日期时间文本编辑框
     * @param property:             0为日期时间类型:yyyy-MM-dd HH:mm:ss
     *                          1为日期类型:yyyy-MM-dd
     *                          2为时间类型:HH:mm:ss
     * @return
     */

    public AlertDialog dateTimePicKDialog(final TextView dateTimeTextEdite, final Property  property) {
        Calendar c = Calendar.getInstance();
        LinearLayout dateTimeLayout = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.date_time_picker, null);
        yearPicker = (NumberPicker) dateTimeLayout.findViewById(R.id.yearpicker);
        monthPicker = (NumberPicker) dateTimeLayout.findViewById(R.id.monthpicker);
        dayPicker = (NumberPicker) dateTimeLayout.findViewById(R.id.daypicker);
        hourPicker = (NumberPicker) dateTimeLayout.findViewById(R.id.hourpicker);
        minutePicker = (NumberPicker) dateTimeLayout.findViewById(R.id.minutepicker);
        init();
        ad = new AlertDialog.Builder(activity).setIcon(R.drawable.icon_modify).setTitle(dateTime).setView(dateTimeLayout).setPositiveButton(R.string.dialog_set,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int whichButton) {
                        dateTimeTextEdite.setText(dateTime);
                        property.setValue(dateTime);
                    }
                }).setNegativeButton(R.string.dialog_btn_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int whichButton) {
                    }
                }).show();
        onDateChanged(null,0,0,0);
        return ad;
    }

    private void init() {
        Calendar calendar = Calendar.getInstance();
        dateTime = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" +
                calendar.get(Calendar.DAY_OF_MONTH) + " " +
                calendar.get(Calendar.HOUR_OF_DAY) + ":" +
                calendar.get(Calendar.MINUTE);
        yearPicker.setFormatter(this);
        yearPicker.setOnValueChangedListener(this);
        yearPicker.setOnScrollListener(this);
        yearPicker.setMaxValue(2100);
        yearPicker.setMinValue(1900);
        yearPicker.setValue(calendar.get(Calendar.YEAR));

        monthPicker.setFormatter(this);
        monthPicker.setOnValueChangedListener(this);
        monthPicker.setOnScrollListener(this);
        monthPicker.setMaxValue(12);
        monthPicker.setMinValue(1);
        monthPicker.setValue(calendar.get(Calendar.MONTH));

        dayPicker.setFormatter(this);
        dayPicker.setOnValueChangedListener(this);
        dayPicker.setOnScrollListener(this);
        dayPicker.setMaxValue(31);
        dayPicker.setMinValue(1);
        dayPicker.setValue(calendar.get(Calendar.DAY_OF_MONTH));


        hourPicker.setFormatter(this);
        hourPicker.setOnValueChangedListener(this);
        hourPicker.setOnScrollListener(this);
        hourPicker.setMaxValue(23);
        hourPicker.setMinValue(0);
        hourPicker.setValue(calendar.get(Calendar.HOUR_OF_DAY));

        minutePicker.setFormatter(this);
        minutePicker.setOnValueChangedListener(this);
        minutePicker.setOnScrollListener(this);
        minutePicker.setMaxValue(59);
        minutePicker.setMinValue(0);
        minutePicker.setValue(calendar.get(Calendar.MINUTE));
    }

    /**
     * 根据年 月 获取对应的月份 天数
     * */
    private   int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
    public String format(int value) {
        String tmpStr = String.valueOf(value);
        if (value < 10) {
            tmpStr = "0" + tmpStr;
        }
        return tmpStr;
    }


    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        onDateChanged(null, 0, 0, 0);
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(yearPicker.getValue(), monthPicker.getValue(),
                dayPicker.getValue(), hourPicker.getValue(),
                minutePicker.getValue());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dateTime = sdf.format(calendar.getTime());
        ad.setTitle(dateTime);
    }

    @Override
    public void onScrollStateChange(NumberPicker view, int scrollState) {

    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.yearpicker:
            case R.id.monthpicker:
                int year=yearPicker.getValue();
                int month=monthPicker.getValue();
                int days=getDaysByYearMonth(year,month);
                dayPicker.setMaxValue(days);
                break;
            case R.id.daypicker:
                break;
            case R.id.hourpicker:
                break;
            case R.id.minutepicker:
                break;
        }
        onDateChanged(null,0,0,0);
    }
}
